package com.itaem.community.modules.infrastructure.company.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itaem.community.core.exception.data.FailOperationException;
import com.itaem.community.core.exception.data.NoSuchDataException;
import com.itaem.community.modules.infrastructure.company.company.dto.CompanyDTO;
import com.itaem.community.modules.infrastructure.company.company.entity.PropertyManagementCompanyEntity;
import com.itaem.community.modules.infrastructure.company.company.mapper.CompanyTransformer;
import com.itaem.community.modules.infrastructure.company.company.mapper.PropertyManagementCompanyDAO;
import com.itaem.community.modules.infrastructure.company.company.service.PropertyManagementCompanyService;
import com.itaem.community.modules.infrastructure.company.department.entity.DepartmentEntity;
import com.itaem.community.modules.infrastructure.company.department.mapper.DepartmentDAO;
import com.itaem.community.modules.infrastructure.company.department.mapper.DepartmentTransformer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

/**
 * <p>
 * 物业管理公司 服务实现类
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Slf4j
@Service
public class PropertyManagementCompanyServiceImpl extends ServiceImpl<PropertyManagementCompanyDAO, PropertyManagementCompanyEntity> implements PropertyManagementCompanyService {

    @Autowired
    private PropertyManagementCompanyDAO propertyManagementCompanyDAO;

    @Autowired
    private CompanyTransformer companyTransformer;

    @Autowired
    private DepartmentDAO departmentDAO;

    @Autowired
    private DepartmentTransformer departmentTransformer;

    @Override
    public CompanyDTO findCompanyById(Integer companyId) {
        PropertyManagementCompanyEntity entity = propertyManagementCompanyDAO.selectById(companyId);
        // 检查数据库
        if (entity == null) {
            throw new NoSuchDataException("不存在您指定的物业公司");
        }

        return companyTransformer.entityToDto(
                entity,
                // 查询此物业公司其下的所有部门信息
                departmentDAO.selectList(
                        new QueryWrapper<DepartmentEntity>()
                                .lambda()
                                .eq(DepartmentEntity::getCompanyId, companyId)
                )
                        // 利用mapstruct提取信息，做实体转换
                        .stream()
                        .map(departmentEntity -> {
                            log.info(departmentEntity.toString());
                            return departmentTransformer.entityToBriefDto(departmentEntity);
                        })
                        .collect(Collectors.toList())
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeCompanyById(Integer companyId) {
        // 对公司执行逻辑删除
        propertyManagementCompanyDAO.deleteById(companyId);
        // 对部门执行逻辑删除
        departmentDAO.delete(
                new QueryWrapper<DepartmentEntity>()
                        .lambda().eq(DepartmentEntity::getCompanyId, companyId)
        );
        if (propertyManagementCompanyDAO.selectCount(
                new QueryWrapper<PropertyManagementCompanyEntity>()
                        .lambda()
                        .eq(PropertyManagementCompanyEntity::getId, companyId)) != 0
                ||
                departmentDAO.selectCount(
                        new QueryWrapper<DepartmentEntity>()
                                .lambda()
                                .eq(DepartmentEntity::getCompanyId, companyId)) != 0) {
            throw new FailOperationException();
        }
        return Boolean.TRUE;
    }
}
